/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.core.hibernate;

import com.arg.deploy.bean.TagLookupCatalog;
import com.arg.deploy.core.common.CoreContent;
import com.arg.deploy.core.common.TransactionalProcessor;
import java.util.HashMap;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sorasaks
 */
public class TagLookupCatalogCore extends CoreContent {

    private static final Logger LOG = Logger.getLogger(TagLookupCatalogCore.class);

    public static TagLookupCatalog getLookupByName(final String tagName) {
        final HashMap param = new HashMap();

        try {
            new TransactionalProcessor() {
                @Override
                public void process(Session session, Transaction tx) throws Exception {
                    Criteria criteria = session.createCriteria(TagLookupCatalog.class);
                    criteria.add(Restrictions.eq("tagName", tagName));
                    criteria.setMaxResults(1);

                    TagLookupCatalog lookup = (TagLookupCatalog) criteria.uniqueResult();
                    param.put(VALUE, lookup);
                }
            }.process();
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            param.put(VALUE, null);
        }

        return (TagLookupCatalog) param.get(VALUE);
    }
}
